/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package modele;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author Delphine
 */
class Annee_session {
    private String id_annee_session;
    private String debut_semestre1;
    private String fin_semestre1;
    private String debut_semestre2;
    private String fin_semestre2;
    private ArrayList<Session_diplome> session_diplome;

    public Annee_session(String id_annee_session, String debut_semestre1, String fin_semestre1, String debut_semestre2, String fin_semestre2, ArrayList<Session_diplome> session_diplome) {
        this.id_annee_session = id_annee_session;
        this.debut_semestre1 = debut_semestre1;
        this.fin_semestre1 = fin_semestre1;
        this.debut_semestre2 = debut_semestre2;
        this.fin_semestre2 = fin_semestre2;
        this.session_diplome = session_diplome;
    }

    public String getDebut_semestre1() {
        return debut_semestre1;
    }

    public String getDebut_semestre2() {
        return debut_semestre2;
    }

    public String getFin_semestre1() {
        return fin_semestre1;
    }

    public String getFin_semestre2() {
        return fin_semestre2;
    }

    public String getId_annee_session() {
        return id_annee_session;
    }

    public ArrayList<Session_diplome> getSession_diplome() {
        return session_diplome;
    }



     /** Obtenir l'annee_session d'id id_annee_session. Null si pas trouvé.
   *
   * @param id_annee_session
   * @return
   * @throws java.sql.SQLException
   */
    public static Annee_session getById(String id_annee_session) throws SQLException{
        Annee_session result = null;
        Connection connexion = null;
        Statement select = null;
        ResultSet rs = null;
        try {
          connexion = BD.connect();
          select = connexion.createStatement();
          String sql = "SELECT * FROM annee_session WHERE id_annee_session='"
              + id_annee_session+"'";
          rs = select.executeQuery(sql);
          if (rs.next()) {
            result = new Annee_session(rs.getString("id_annee_session"), (""+rs.getTimestamp("debut_semestre1")).substring(0, 10),(""+rs.getTimestamp("fin_semestre1")).substring(0, 10),(""+rs.getTimestamp("debut_semestre2")).substring(0, 10),(""+rs.getTimestamp("fin_semestre2")).substring(0, 10), getSessionDiplome(rs.getString("id_annee_session")));
          }
        } finally {
            BD.close(rs);
            BD.close(select);
            BD.close(connexion);
        }
        return result;
    }

    /** Obtenir les session_diplome d'id id_annee_session. Null si pas trouvé.
   *
   * @param id_annee_session
   * @return
   * @throws java.sql.SQLException
   */
    public static ArrayList<Session_diplome> getSessionDiplome(String id_annee_session) throws SQLException{
        Connection connexion = null;
        Statement select = null;
        ResultSet rs = null;
        ArrayList<Session_diplome> session_diplome = null;
        try {
          connexion = BD.connect();
          select = connexion.createStatement();
          String sql = "SELECT * FROM session_diplome WHERE id_annee_session ='" + id_annee_session + "'";
          rs = select.executeQuery(sql);
          Session_diplome s=null;
          session_diplome = new ArrayList<Session_diplome>();
          while (rs.next() != false) {
            s = new Session_diplome(rs.getInt("id_session"),rs.getInt("id_version_diplome"),rs.getString("id_formation"),rs.getString("nom"),(""+rs.getTimestamp("date_debut")).substring(0, 10),(""+rs.getTimestamp("date_fin")).substring(0, 10), Formateur.getById(rs.getInt("id_session")),rs.getString("id_annee_session"), Session_diplome.getProjetsID(rs.getInt("id_session")));
            session_diplome.add(s);
          }
        } finally {
            BD.close(rs);
            BD.close(select);
            BD.close(connexion);
        }
        return session_diplome;
    }

    @Override
    public boolean equals(Object annee_session){
        return (id_annee_session.equals(((Annee_session)annee_session).getId_annee_session())
                && debut_semestre1.equals(((Annee_session)annee_session).getDebut_semestre1())
                && fin_semestre1.equals(((Annee_session)annee_session).getFin_semestre1())
                && debut_semestre2.equals(((Annee_session)annee_session).getDebut_semestre2())
                && fin_semestre2.equals(((Annee_session)annee_session).getFin_semestre2())
                && session_diplome.equals(((Annee_session)annee_session).getSession_diplome()));
    }
}
